var extension = "";

$(document).ready(function(){
	showBasicInfo();
});

function showModifyEmailDlg() {
	var requestUrl = contextPath + "/customer/modify_email.jsp";
	$.post(requestUrl, "", function(data){
		addCover(contextPath);
		$(document.body).append(data);
	});
}

function cancelModifyEmailDlg() {
	$("#div_modify_email").remove();
	$(cover).remove();
}

function showModifyPasswordDlg() {
	var requestUrl = contextPath + "/customer/modify_password.jsp";
	$.post(requestUrl, "", function(data){
		addCover(contextPath);
		$(document.body).append(data);
	});
}

function cancelModifyPasswordDlg() {
	$("#div_modify_password").remove();
	$(cover).remove();
}

function showBasicInfo() {
	requestUrl = "customer_json_servlet";
	var params = {
		request_method:"get_profile"
	};
	
	$.post(requestUrl, params, function(data){
		if (data.code == 0) {
			var innerHtml = "";
			var profile = data.resultData;
			
			innerHtml += '<div style="width: 390px; height: 50px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;">';
			innerHtml += '	<span style="padding-top: 14px;">基本信息</span>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">ID(昵称):</span>';
			innerHtml += '	<span>' + profile.id + '</span>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">密码:</span>';
			innerHtml += '	<span>******</span>';
			innerHtml += '	<a href="javascript: showModifyPasswordDlg();" style="margin-left: 20px; font-size: 13pt;">修改</a>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">邮箱:</span>';
			innerHtml += '	<span>' + profile.email + '</span>';
			innerHtml += '	<a href="javascript: showModifyEmailDlg();" style="margin-left: 20px; font-size: 13pt;">修改</a>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">手机:</span>';
			innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);"';
			innerHtml += '	id="mobile" value="' + profile.mobile + '"/>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">城市:</span>';
			innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);"';
			innerHtml += '		id="city" value="' + profile.city + '"/>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
			innerHtml += '	<span style="width: 115px; display: inline-block;">旺旺:</span>';
			innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);"';
			innerHtml += '		id="wangwang" value="' + profile.wangwang + '"/>';
			innerHtml += '</div>';
			innerHtml += '<div style="padding-top: 60px;" align="center">';
			innerHtml += '	<img class="pointer fade_effect" src="../image/btn_save.png" onclick="modifyProfile();">';
			innerHtml += '</div>';
				
			$("#div_setting_detail").html(innerHtml);
			addFadeEffect();
		}
	}, "json");
}

function showCarInfo() {
	requestUrl = "customer_json_servlet";
	var params = {
		request_method:"get_my_cars_detail"
	};
	
	$.post(requestUrl, params, function(data){
		try {
			if (data.code == 0) {
				var innerHtml = "";

				innerHtml += '<div style="width: 390px; height: 50px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;">';
				innerHtml += '	<span style="padding-top: 14px;">车辆信息</span>';
				innerHtml += '</div>';
				
				for (var i = 0; i < data.resultData.length; i++) {
					var carInfo = data.resultData[i];
					
					innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
					innerHtml += '	<span style="width: 115px; display: inline-block;">车牌号:</span>';
					innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
					innerHtml += '		id="license' + i + '" value="' + carInfo.carLicense + '" disabled="disabled"/>';
					innerHtml += '</div>';
					innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
					innerHtml += '	<span style="width: 115px; display: inline-block;">车型:</span>';
					innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
					innerHtml += '		id="model' + i + '" value="' + carInfo.carModel + '"/>';
					innerHtml += '</div>';
					innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
					innerHtml += '	<span style="width: 115px; display: inline-block;">排量:</span>';
					innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
					innerHtml += '		id="displacement' + i + '" value="' + carInfo.carDisplacement + '"/>';
					innerHtml += '</div>';
					innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
					innerHtml += '	<span style="width: 115px; display: inline-block;">购买年月:</span>';
					innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
					innerHtml += '	id="buy_date' + i + '" value="' + carInfo.buyDate + '"/>';
					innerHtml += '</div>';
					innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
					innerHtml += '	<input type="hidden" id="image_name' + i + '"/>';
					innerHtml += '	<img id="img_car' + i + '" src="' + WSContextPath + "/" + carInfo.carPhoto + '" width="100px" height="105px" align="top"/>';
					innerHtml += '	<object id="flash_upload" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540002" onmouseover="extension = ' + i + ';"'; 
					innerHtml += '		type="application/x-shockwave-flash" width="119" height="34" style="margin-left: 50px;">';
					innerHtml += '	  <param name="movie" value="<%= request.getContextPath() %>/flash/upload_car.swf?ws_context=' + WSContextPath + '" />';
					innerHtml += '	  <param name="quality" value="high" />';
					innerHtml += '	  <param name="wmode" value="transparent"/>';
					innerHtml += '	  <embed src="../flash/upload_car.swf?ws_context=' + WSContextPath + '" ';
					innerHtml += '	  	wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
					innerHtml += '	  	allowScriptAccess="sameDomain" allowFullScreen="false"';
					innerHtml += '	  	type="application/x-shockwave-flash" width="119" height="34">';
					innerHtml += '	  </embed>';
					innerHtml += '	</object>';
					innerHtml += '</div>';
					innerHtml += '<div style="padding-top: 15px;" align="center">';
					innerHtml += '	<img class="pointer fade_effect" src="../image/btn_save.png" onclick="modifyCarInfo(' + i + ');">';
					innerHtml += '	<img class="pointer fade_effect" src="../image/btn_delete3.png" onclick="modifyProfile();">';
					innerHtml += '</div>';
					innerHtml += '<div style="width: 390px; height: 50px; font-size: 15pt; font-weight: bold; border-bottom: 1px solid #c8c8c8;"></div>';
				}
				
				innerHtml += '<div id="div_add_button" style="padding-top: 15px;">';
				innerHtml += '	<img class="pointer fade_effect" src="../image/btn_add_car.png" onclick="addCarDiv();">';
				innerHtml += '</div>';
					
				$("#div_setting_detail").html(innerHtml);
				addFadeEffect();

				if (data.resultData.length > 1) {
					$("#scroll_bottom").css("display", "");
				}
			}
		
		} catch (e) {
			showTestMessage(e);
		}
	}, "json");
}

function addCarDiv() {
	if ($("#div_new_car").is("#div_new_car")) {
		showMessage("请先保存");
		return;
	}
	
	var innerHtml = "";
	innerHtml += '<div id="div_new_car">';
	innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
	innerHtml += '	<span style="width: 115px; display: inline-block;">车牌号:</span>';
	innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
	innerHtml += '		id="license_new" value=""/>';
	innerHtml += '</div>';
	innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
	innerHtml += '	<span style="width: 115px; display: inline-block;">车型:</span>';
	innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
	innerHtml += '		id="model_new" value=""/>';
	innerHtml += '</div>';
	innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
	innerHtml += '	<span style="width: 115px; display: inline-block;">排量:</span>';
	innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
	innerHtml += '		id="displacement_new" value=""/>';
	innerHtml += '</div>';
	innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
	innerHtml += '	<span style="width: 115px; display: inline-block;">购买年月:</span>';
	innerHtml += '	<input style="width: 251px; height: 44px; border: none; background: url(../image/bg_text_blank.png);" ';
	innerHtml += '	id="buy_date_new" value=""/>';
	innerHtml += '</div>';
	innerHtml += '<div style="padding-top: 15px; font-size: 14pt;">';
	innerHtml += '	<input type="hidden" id="image_name_new"/>';
	innerHtml += '	<img id="img_car_new" src="" width="100px" height="105px" align="top"/>';
	innerHtml += '	<object id="flash_upload" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540002" onmouseover="extension = \'_new\';"'; 
	innerHtml += '		type="application/x-shockwave-flash" width="119" height="34" style="margin-left: 50px;">';
	innerHtml += '	  <param name="movie" value="<%= request.getContextPath() %>/flash/upload_car.swf?ws_context=' + WSContextPath + '" />';
	innerHtml += '	  <param name="quality" value="high" />';
	innerHtml += '	  <param name="wmode" value="transparent"/>';
	innerHtml += '	  <embed src="../flash/upload_car.swf?ws_context=' + WSContextPath + '" ';
	innerHtml += '	  	wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
	innerHtml += '	  	allowScriptAccess="sameDomain" allowFullScreen="false"';
	innerHtml += '	  	type="application/x-shockwave-flash" width="119" height="34">';
	innerHtml += '	  </embed>';
	innerHtml += '	</object>';
	innerHtml += '</div>';
	innerHtml += '<div style="padding-top: 15px;" align="center">';
	innerHtml += '	<img class="pointer fade_effect" src="../image/btn_save.png" onclick="addCarInfo();">';
	innerHtml += '	<img class="pointer fade_effect" src="../image/btn_delete3.png" onclick="cancelAdd();">';
	innerHtml += '</div>';
	innerHtml += '</div>';
	
	$("#div_add_button").before(innerHtml);
	scrollToBottom();
}

function cancelAdd() {
	$("#div_new_car").remove();
}

function modifyProfile() {
	requestUrl = "customer_json_servlet";
	var params = {
		request_method:"modify_profile",
		mobile:$("#mobile").val(),
		city:$("#city").val(),
		wangwang:$("#wangwang").val()
	};
	
	$.post(requestUrl, params, function(data){
		if (data.code == 0) {
			showMessage("修改成功");
			showCarInfo();
		}
	}, "json");
}

function addCarInfo() {
	requestUrl = "customer_json_servlet";
	var params = {
		request_method:"add_car_info",
		car_license:$("#license_new").val(),
		car_model:$("#model_new").val(),
		displacement:$("#displacement_new").val(),
		buy_date:$("#buy_date_new").val(),
		photo:$("#image_name_new").val()
	};
	
	$.post(requestUrl, params, function(data){
		if (data.code == 0) {
			showMessage("添加成功");
		}
	}, "json");
}

function modifyCarInfo(index) {
	requestUrl = "customer_json_servlet";
	var params = {
		request_method:"modify_car_info",
		car_license:$("#license" + index).val(),
		car_model:$("#model" + index).val(),
		displacement:$("#displacement" + index).val(),
		buy_date:$("#buy_date" + index).val(),
		photo:$("#image_name" + index).val()
	};
	
	$.post(requestUrl, params, function(data){
		if (data.code == 0) {
			showMessage("修改成功");
		}
	}, "json");
}

function scrollToBottom() {
	$("#div_setting_detail").scrollTop($("#div_setting_detail").scrollTop() + 450);
	if ($("#div_setting_detail").scrollTop() > 0) {
		$("#scroll_bottom").css("marginTop", "0px");
		$("#scroll_top").css("display", "");
	}
}

function scrollToTop() {
	$("#div_setting_detail").scrollTop($("#div_setting_detail").scrollTop() - 450);
	if ($("#div_setting_detail").scrollTop() == 0) {
		$("#scroll_bottom").css("marginTop", "25px");
		$("#scroll_top").css("display", "none");
	}
}

function freshImage(data) {
	var result = eval('(' + data + ')');
	var imageName = "";

	if (result.code == "0") {
		imageName = result.resultData;
		document.getElementById("img_car" + extension).src = WSContextPath + "/image/uploads/" + imageName;
	} else {
		showMessage(result.message);
	}
	document.getElementById("image_name" + extension).value = imageName;
}